/*
 * This file (AkEllipse.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */

package alkwarel.shapes;

import alkwarel.objects.AkArtspace;
import alkwarel.objects.AkShape;
import artx.utils.AxLog;
import artx.utils.AxRandom;

/**
 * Draws an ellipse.
 * 
 * @author Xavier Gouchet
 * 
 */
public class AkEllipse extends AkShape {

	/**
	 * Standard Constructor.
	 */
	public AkEllipse() {
		super(1, "Ellipse",null);
	}

	/**
	 * @see alkwarel.objects.AkShape#Draw(alkwarel.objects.AkArtspace)
	 */
	public boolean Draw(AkArtspace a) {
		AxLog.Debug("AkEllipse::Draw =>> applying an Ellipse on an artspace");
		float x,y,w,h;
		x = AxRandom.RandF(a.width);
		y = AxRandom.RandF(a.height);
		w = AxRandom.RandF(Math.min(x,a.width-x));
		h = AxRandom.RandF(Math.min(y,a.height-y));
		
		AxLog.Debug("AkEllipse::Draw =>> Drawing ellipse "+x+";"+y+" ("+w+";"+h+")");
		int steps = 32;
		float x1 = 0,x2,y1 = 0,y2;
		for (int i=0; i<=steps; i++){
			double angle = i * ((2*Math.PI) / steps);
			if (i==0){
				x1 = x + (float) (Math.cos(angle)*w);
				y1 = y + (float) (Math.sin(angle)*h);
			}else{
				x2 = x + (float) (Math.cos(angle)*w);
				y2 = y + (float) (Math.sin(angle)*h);
				a.DrawStroke(x1, y1, x2, y2);
				x1 = x2;
				y1 = y2;
			}
		}
		return true;
	}

}
